package com.hengtongxing.hejiayun.homepage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.BaseActivity;
import com.hengtongxing.hejiayun.base.DataCallBack;
import com.hengtongxing.hejiayun.bean.AddressListBean;
import com.hengtongxing.hejiayun.bean.AliPayDataBean;
import com.hengtongxing.hejiayun.bean.OrderDetailsBean;
import com.hengtongxing.hejiayun.bean.OrderListBean;
import com.hengtongxing.hejiayun.bean.PrepareConfigBean;
import com.hengtongxing.hejiayun.bean.RefundRecordBean;
import com.hengtongxing.hejiayun.homepage.adapter.HomeGoodsListAdapter;
import com.hengtongxing.hejiayun.homepage.adapter.ImgListAdapter;
import com.hengtongxing.hejiayun.mine.model.IMineModel;
import com.hengtongxing.hejiayun.mine.model.MineModelImpl;
import com.hengtongxing.hejiayun.shop.ShopGoodsDetailsActivity;
import com.hengtongxing.hejiayun.shop.ShopOrderDetailsActivity;
import com.hengtongxing.hejiayun.shop.adapter.OrderProductListAdapter;
import com.hengtongxing.hejiayun.utils.DateTimeUtil;
import com.hengtongxing.hejiayun.utils.Dp2Px;
import com.hengtongxing.hejiayun.utils.NumberFormatUtils;
import com.hengtongxing.hejiayun.utils.StringUtils;
import com.hengtongxing.hejiayun.widget.AppTitleBar;
import com.hengtongxing.hejiayun.widget.CommonDialog;
import com.hengtongxing.hejiayun.widget.SupplementDialog;
import com.hengtongxing.hejiayun.widget.ViewLoading;
import com.hengtongxing.hejiayun.widget.WrapContentLinearLayoutManager;

import java.util.Date;
import java.util.List;

public class RefundDetailsActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_order_goods_id)
    TextView tv_order_goods_id;
    @BindView(R.id.tv_shop_order_id)
    TextView tv_shop_order_id;
    @BindView(R.id.tv_goods_status)
    TextView tv_goods_status;
    @BindView(R.id.tv_remark)
    TextView tv_remark;
    @BindView(R.id.tv_refund_amount)
    TextView tv_refund_amount;
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.tv_depict)
    TextView tv_depict;
    @BindView(R.id.tv_create_date)
    TextView tv_create_date;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_commit)
    TextView tv_commit;

    private int order_goods_id;
    private int shop_order_id;
    private int goods_status;
    private int status;
    private String remark;
    private int refund_amount;
    private String depict;
    private String create_date;
    private List<String> ps;
    private int position;
    private IMineModel mineModel;
    private OrderListBean.DataBean.ItemListBean.ShopOrderGoodsBean shopOrderGoodsBean;
    private int id;
    private String delivery_no;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_details);
        ButterKnife.bind(this);
        mineModel = new MineModelImpl();
        initView();
        initData();
        initEvent();
    }

    private void initView() {

        Intent intent = getIntent();

        position = intent.getIntExtra("position", 0);
        id = intent.getIntExtra("id", 0);
        order_goods_id = intent.getIntExtra("order_goods_id", 0);
        shop_order_id = intent.getIntExtra("shop_order_id", 0);
        goods_status = intent.getIntExtra("goods_status", 0);
        status = intent.getIntExtra("status", 0);
        remark = intent.getStringExtra("remark");
        refund_amount = intent.getIntExtra("refund_amount", 0);
        depict = intent.getStringExtra("depict");
        create_date = intent.getStringExtra("create_date");
        delivery_no = intent.getStringExtra("delivery_no");
        String pic = intent.getStringExtra("pic");


        ps = new Gson().fromJson(pic, new TypeToken<List<String>>() {
        }.getType());

        if (position == 0 && status == 1 && "".equals(StringUtils.nullString(delivery_no))) {
            tv_commit.setText("添加快递单号");
            tv_commit.setVisibility(View.VISIBLE);
        }
        if (position == 0 && (status == 3 || status == 4)) {
            //退款
            tv_commit.setVisibility(View.VISIBLE);
        }


    }

    private void initData() {

        tv_order_goods_id.setText("商品订单id：" + order_goods_id);
        tv_shop_order_id.setText("订单id：" + shop_order_id);


        if (goods_status == 1) {
            tv_goods_status.setText("货物状态：未收到货");
        } else {
            tv_goods_status.setText("货物状态：已收到货");
        }

        tv_remark.setText("退款原因：" + remark);
        tv_refund_amount.setText("退款金额：" + refund_amount/100 + "元");

        if (status == 1) {
            tv_status.setText("退款状态：已申请");
        } else if (status == 2) {
            tv_status.setText("退款状态：商家已同意");
        } else if (status == 3) {
            tv_status.setText("退款状态：商家拒绝");
        } else if (status == 4) {
            tv_status.setText("退款状态：退款失败");
        } else if (status == 5) {
            tv_status.setText("退款状态：退款成功");
        } else {
            tv_status.setText("退款状态：未知");
        }
        tv_depict.setText("补充描述：" + depict);
        tv_create_date.setText("创建时间：" + create_date);


        requestOrderShow();
    }

    private void initEvent() {
        ImgListAdapter listAdapter = new ImgListAdapter(ps);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new GridLayoutManager(mActivity, 3));
        recyclerView.setAdapter(listAdapter);

        listAdapter.setOnCallBackListener(position -> {

        });
        tv_commit.setOnClickListener(v -> {

            if (position == 0 && (status == 3 || status == 4)) {
                //退款
                showPictureSelectorDialog();
            }

            if (position == 0 && status == 1 && "".equals(StringUtils.nullString(delivery_no))) {
                //添加快递单号
                addDeliveryNo();
            }

        });
    }


    public void requestOrderShow() {
        mineModel.requestOrderShow(String.valueOf(shop_order_id), "order_by_me", new DataCallBack<OrderDetailsBean>() {
            @Override
            public void onSuccessful(OrderDetailsBean bean) {
                OrderListBean.DataBean.ItemListBean dataBean = bean.getData();
                if (dataBean != null) {
                    List<OrderListBean.DataBean.ItemListBean.ShopOrderGoodsBean> shop_order_goods = dataBean.getShop_order_goods();

                    for (int i = 0; i < shop_order_goods.size(); i++) {
                        if (shop_order_goods.get(i).getId() == order_goods_id) {
                            shopOrderGoodsBean = shop_order_goods.get(i);
                        }
                    }
                }

                String s = new Gson().toJson(shopOrderGoodsBean);

                Log.e("商品=", s);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private void showPictureSelectorDialog() {
        BottomSheetDialog selectPicDialog = new BottomSheetDialog(this, R.style.Dialog);
        selectPicDialog.getWindow().getAttributes().windowAnimations = R.style.commomDialog;
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_cargo_status, null);
        //退款
        TextView tv_refund = view.findViewById(R.id.tv_refund);
        //退货退款
        TextView tv_return_refund = view.findViewById(R.id.tv_return_refund);
        //取消
        TextView cancel = view.findViewById(R.id.tv_select_pictrue_cancel);

        tv_refund.setOnClickListener(v -> {
            Intent intent = new Intent(this, ApplyForRefundActivity.class);
            intent.putExtra("type", "0");//退款
            intent.putExtra("shop_goods_thumb", shopOrderGoodsBean.getShop_goods_thumb());
            intent.putExtra("shop_goods_name", shopOrderGoodsBean.getShop_goods_name());
            intent.putExtra("norm_name", shopOrderGoodsBean.getNorm_name());
            intent.putExtra("shop_goods_price", NumberFormatUtils.formatDouble(Double.parseDouble(shopOrderGoodsBean.getShop_goods_price()) / 100));
            intent.putExtra("count", "x" + shopOrderGoodsBean.getCount() + "");
            intent.putExtra("order_status", goods_status);
            intent.putExtra("order_goods_id", shopOrderGoodsBean.getShop_goods_id());
            intent.putExtra("id", shopOrderGoodsBean.getId());
            startActivity(intent);
            selectPicDialog.dismiss();
            finish();
        });
        tv_return_refund.setOnClickListener(v -> {

            Intent intent = new Intent(this, ApplyForRefundActivity.class);
            intent.putExtra("type", "1");//退款退货
            intent.putExtra("shop_goods_thumb", shopOrderGoodsBean.getShop_goods_thumb());
            intent.putExtra("shop_goods_name", shopOrderGoodsBean.getShop_goods_name());
            intent.putExtra("norm_name", shopOrderGoodsBean.getNorm_name());
            intent.putExtra("shop_goods_price", NumberFormatUtils.formatDouble(Double.parseDouble(shopOrderGoodsBean.getShop_goods_price()) / 100));
            intent.putExtra("count", "x" + shopOrderGoodsBean.getCount() + "");
            intent.putExtra("order_status", goods_status);
            intent.putExtra("order_goods_id", shopOrderGoodsBean.getShop_goods_id());
            intent.putExtra("id", shopOrderGoodsBean.getId());
            startActivity(intent);
            selectPicDialog.dismiss();
            finish();
        });
        cancel.setOnClickListener(v -> {
            selectPicDialog.dismiss();
        });

        selectPicDialog.setContentView(view);
        selectPicDialog.show();
    }


    private void addDeliveryNo() {
        showDialog();

    }

    public void showDialog() {
        SupplementDialog.Builder dialogBuild = new SupplementDialog.Builder(this);
        dialogBuild.setTitle("输入快递单号");
        ;
        dialogBuild.setPositiveButton("确定", v -> {
            String content = dialogBuild.getContent();
            dialogBuild.dismiss();
            mineModel.addDeliveryNo(id, content, new DataCallBack<AliPayDataBean>() {
                @Override
                public void onSuccessful(AliPayDataBean bean) {
                    finish();
                }

                @Override
                public void onFailed(String msg, String code) {

                }
            });
        });

        dialogBuild.setNegativeButton("取消", v -> dialogBuild.dismiss());
        dialogBuild.setCancelable(false);
        dialogBuild.show();

    }

}